package com.example240702;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.web.bind.annotation.*;

import java.util.HashSet;
import java.util.Objects;

@RestController
@SpringBootApplication
public class demoGetPost {

    public static void main(String[] args) {
        SpringApplication.run(demoGetPost.class, args);
    }
}

@RestController
@RequestMapping(path = "/get")
class GetResponse {
    @GetMapping()//tested
    //return String "hello! ${info}"
    private String demoGet(@RequestParam("info") String info) {
        return "hello! " + info;
    }

    @GetMapping("/{name}/{id}")//tested
    //return json{"name":"${name},"id":"${id}"}
    private Person demoGet2(@PathVariable("name") String name, @PathVariable("id") String id) {
        return new Person(name, id);
    }

    @GetMapping("/query/{name}/{id}")//tested
    //true if person exists, else false
    private boolean demoGetQuery(@PathVariable("name") String name, @PathVariable("id") String id) {
        return People.query(new Person(name, id));
    }

    @GetMapping("/show")//tested
    //show all known people
    private String demoGetShow() {
        if (People.isEmpty()) {
            return "nobody";
        } else {
            return People.all();
        }
    }

}

@RestController
@RequestMapping(path = "/post")
class PostResponse {
    @PostMapping("/add")//tested
    //send json{"name":"${name},"id":"${id}"}, and add the new person information
    private String demoPostAdd(@RequestBody Person somebody) {
        People.add(somebody);
        return somebody.name + "'s id is " + somebody.id;
    }

    @PostMapping("/query")//tested
    //send json{"name":"${name},"id":"${id}"}, true if exits
    private boolean demoPostExist(@RequestBody Person somebody) {
        return People.query(somebody);
    }

    @PostMapping("/remove")//tested
    //send json{"name":"${name},"id":"${id}"}, true if remove person successfully
    private boolean demoPostRemove(@RequestBody Person somebody) {
        return People.remove(somebody);
    }

}

class Person {
    public String name;
    public String id;

    public Person(String name, String id) {
        this.name = name;
        this.id = id;
    }

    @Override
    public boolean equals(Object o) {
        if (o instanceof Person p) {
            return Objects.equals(this.name, p.name);
        }
        return false;
    }

    @Override
    public int hashCode() {
        return Objects.hash(this.id);
    }
}

class People {
    private static final HashSet<Person> known_people = new HashSet<>();

    public static void add(Person p) {
        known_people.add(p);
    }

    public static boolean query(Person p) {
        return known_people.contains(p);
    }

    public static boolean remove(Person p) {
        return known_people.remove(p);
    }

    public static String all() {
        StringBuilder sb = new StringBuilder();
        for (Person p : known_people) {
            sb.append(p.id + " : " + p.name + "\n");
        }
        return sb.toString();
    }

    public static boolean isEmpty() {
        return known_people.isEmpty();
    }
}